<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="案件号" prop="evtCode">
        <el-input
          style="width: 219px"
          v-model="queryParams.evtCode"
          placeholder="请输入案件号"
          clearable
          size="small"
          oninput="value=value.replace(/^\.+|[^\d.]/g,'')"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="案件类型" prop="evtType">
        <el-select v-model="queryParams.evtType" placeholder="请选择案件类型" clearable size="small">
          <el-option
            v-for="dict in evtTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="案件来源" prop="evtSource">
        <el-select v-model="queryParams.evtSource" placeholder="请选择案件来源" clearable size="small">
          <el-option
            v-for="dict in evtSourceOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="案件状态" prop="evtState">
        <el-select v-model="queryParams.evtState" placeholder="请选择案件状态" clearable size="small">
          <el-option
            v-for="dict in evtStateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="违法、违规信息" prop="violation" label-width="110px">
        <!--        <el-select v-model="queryParams.violation" placeholder="请选择违法、违规信息" clearable size="small">-->
        <!--          <el-option-->
        <!--            v-for="dict in violationOptions"-->
        <!--            :key="dict.dictValue"-->
        <!--            :label="dict.dictLabel"-->
        <!--            :value="dict.dictValue"-->
        <!--          />-->
        <!--        </el-select>-->
        <treeselect v-model="queryParams.violation" :options="violationOptions" :show-count="true"
                    placeholder="请选择违法、违规信息" style="width:300px;"/>

      </el-form-item>
      <el-form-item label="管辖区" prop="areaName">
        <el-select v-model="queryParams.areaName" placeholder="请选择管辖区" clearable size="small">
          <el-option
            v-for="dict in areaNameOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="举报时间" prop="reporterTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.reporterTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择举报时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="setPrint"
          v-hasPermi="['frontierEvent:event:export']"
        >打印
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['frontierEvent:event:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="eventList" @selection-change="handleSelectionChange" id="tableList">
      <el-table-column type="selection" align="center"/>
      <el-table-column label="主建id" align="center" prop="id" v-if="false"/>
      <el-table-column label="案件号" align="center" prop="evtCode"/>
      <el-table-column label="案件类型" align="center" prop="evtType" :formatter="evtTypeFormat"/>
      <el-table-column label="案件来源" align="center" prop="evtSource" :formatter="evtSourceFormat"/>
      <el-table-column label="案件地点" align="center" prop="evtAddress"/>
      <el-table-column label="案件内容" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span v-html="scope.row.evtContent">{{scope.row.evtContent}}</span>
        </template>
      </el-table-column>
      <el-table-column label="案件状态" align="center" prop="evtState" :formatter="evtStateFormat"/>
      <el-table-column label="违法、违规信息" align="center" prop="violationName"/>
      <el-table-column label="管辖区" align="center" prop="areaName" :formatter="areaNameFormat"/>
      <!--      <el-table-column label="举报人" align="center" prop="reporterObjectName" />-->
      <el-table-column label="举报时间" align="center" prop="reporterTime" width="180">
        <!--        <template slot-scope="scope">-->
        <!--          <span>{{ parseTime(scope.row.reporterTime, '{y}-{m}-{d}') }}</span>-->
        <!--        </template>-->
      </el-table-column>
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--    <template slot-scope="scope">-->
      <!--      <el-button-->
      <!--        size="mini"-->
      <!--        type="text"-->
      <!--        icon="el-icon-edit"-->
      <!--        @click="detail(scope.row.id)"-->
      <!--        v-hasPermi="['frontierEvent:event:detail']"-->
      <!--      >查看</el-button>-->
      <!--    </template>-->
      <!--  </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改案件信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="案件等级" prop="evtLevel">
          <el-input v-model="form.evtLevel" placeholder="请输入案件等级"/>
        </el-form-item>
        <el-form-item label="案件类型" prop="evtType">
          <el-select v-model="form.evtType" placeholder="请选择案件类型">
            <el-option
              v-for="dict in evtTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="案件来源" prop="evtSource">
          <el-select v-model="form.evtSource" placeholder="请选择案件来源">
            <el-option
              v-for="dict in evtSourceOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="案件时间" prop="evtDate">
          <el-date-picker clearable size="small"
                          v-model="form.evtDate"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择案件时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="案件地点" prop="evtAddress">
          <el-input v-model="form.evtAddress" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="案件内容">
          <el-input v-model="form.evtContent" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="案件状态" prop="evtState">
          <el-select v-model="form.evtState" placeholder="请选择案件状态">
            <el-option
              v-for="dict in evtStateOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="违法、违规信息" prop="violation">
          <el-select v-model="form.violation" placeholder="请选择违法、违规信息">
            <el-option
              v-for="dict in violationOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="管辖区" prop="areaName">
          <el-select v-model="form.areaName" placeholder="请选择管辖区">
            <el-option
              v-for="dict in areaNameOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="X坐标" prop="x">
          <el-input v-model="form.x" placeholder="请输入X坐标"/>
        </el-form-item>
        <el-form-item label="Y坐标" prop="y">
          <el-input v-model="form.y" placeholder="请输入Y坐标"/>
        </el-form-item>
        <!--        <el-form-item label="举报人ID" prop="reporterObjectId">-->
        <!--          <el-input v-model="form.reporterObjectId" placeholder="请输入举报人ID" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="举报人" prop="reporterObjectName">-->
        <!--          <el-input v-model="form.reporterObjectName" placeholder="请输入举报人" />-->
        <!--        </el-form-item>-->
        <el-form-item label="举报时间" prop="reporterTime">
          <el-date-picker clearable size="small"
                          v-model="form.reporterTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择举报时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listEvent, getEvent, delEvent, addEvent, updateEvent, exportEvent } from '@/api/frontierEvent/event'
  import Editor from '@/components/Editor'
  import { treeselect } from '@/api/basicInformation/violationType'
  import Treeselect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'

  export default {
    name: 'Event',
    components: {
      Editor,
      Treeselect
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 案件信息表格数据
        eventList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 案件类型字典
        evtTypeOptions: [],
        // 案件来源字典
        evtSourceOptions: [],
        // 案件状态字典
        evtStateOptions: [],
        // 违法、违规信息字典
        violationOptions: [],
        // 管辖区字典
        areaNameOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          evtCode: undefined,
          evtType: undefined,
          evtSource: undefined,
          evtState: undefined,
          violation: undefined,
          areaName: undefined,
          reporterTime: undefined,
          isAsc: 'desc',
          orderByColumn: 'id',
          isExport: true
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          evtSource: [
            { required: true, message: '案件来源不能为空', trigger: 'change' }
          ],
          evtState: [
            { required: true, message: '案件状态不能为空', trigger: 'change' }
          ]
        }
      }
    },
    created() {
      this.getList()
      this.getTreeselect()
      this.getDicts('evt_type').then(response => {
        this.evtTypeOptions = response.data
      })
      this.getDicts('evt_source').then(response => {
        this.evtSourceOptions = response.data
      })
      this.getDicts('evt_state').then(response => {
        this.evtStateOptions = response.data
      })
      // this.getDicts("evt_violation_type").then(response => {
      //   this.violationOptions = response.data;
      // });
      this.getDicts('evt_area').then(response => {
        this.areaNameOptions = response.data
      })
    },
    methods: {
      getTreeselect() {
        treeselect().then(response => {
          this.violationOptions = response.data
        })
      },
      // 筛选节点
      filterNode(value, data) {
        if (!value) return true
        return data.label.indexOf(value) !== -1
      },
      // 节点单击事件
      handleNodeClick(data) {
        this.queryParams.violation = data.id
        this.getList()
      },
      setPrint() {
        var html = document.getElementById('tableList').innerHTML
        var w = window.open(location.href)
        var style = w.document.createElement('style')
        style.type = 'text/css'
        style.innerText = `打印内容元素的样式...`
        // w.document.getElementsByTagName("head").item(0).appendChild(style);
        var tableStr = '<table style=\'width:100%;\'><tr><td style=\'text-align: center;\'>案件类型</td><td style=\'text-align: center;\'>案件来源</td><td style=\'text-align: center;\'>案件地点</td><td style=\'text-align: center;\'>案件内容</td><td style=\'text-align: center;\'>案件状态</td><td style=\'text-align: center;\'>违法、违规信息</td><td style=\'text-align: center;\'>管辖区</td><td style=\'text-align: center;\'>举报时间</td></tr>'
        for (var i = 0; i < this.eventList.length; i++) {
          var evtTypeName = this.selectDictLabel(this.evtTypeOptions, this.eventList[i].evtType)
          var evtSourceName = this.selectDictLabel(this.evtSourceOptions, this.eventList[i].evtSource)
          var evtStateName = this.selectDictLabel(this.evtStateOptions, this.eventList[i].evtState)
          var violationName = this.selectDictLabel(this.violationOptions, this.eventList[i].violation)
          var areaNameName = this.selectDictLabel(this.areaNameOptions, this.eventList[i].areaName)
          var reporterTime = this.eventList[i].reporterTime === null ? '' : this.eventList[i].reporterTime
          tableStr += '<tr><td style=\'text-align: center;\'>' + evtTypeName + '</td><td style=\'text-align: center;\'>' + evtSourceName + '</td><td style=\'text-align: center;\'>' + this.eventList[i].evtAddress + '</td><td style=\'text-align: center;\'>' + this.eventList[i].evtContent + '</td><td style=\'text-align: center;\'>' + evtStateName + '</td><td style=\'text-align: center;\'>' + violationName + '</td><td style=\'text-align: center;\'>' + areaNameName + '</td><td style=\'text-align: center;\'>' + reporterTime + '</td></tr>'
        }
        tableStr += '</table>'
        w.document.body.innerHTML = tableStr
        w.document.close()
        w.print()
        w.close()
      },
      /** 查询案件信息列表 */
      getList() {
        this.loading = true
        if (this.queryParams.reporterTime !== null && this.queryParams.reporterTime !== undefined) {
          let newQueryParams = { ...this.queryParams }
          let newDate = newQueryParams.reporterTime
          newDate = new Date(new Date(newDate).getTime() + 24 * 60 * 60 * 1000 - 1)
          this.queryParams.reporterEndTime = this.dateFormat(newDate)
        }
        listEvent(this.queryParams).then(response => {
          this.eventList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      //时间格式化函数，此处仅针对yyyy-MM-dd hh:mm:ss 的格式进行格式化
      dateFormat(time) {
        let date = new Date(time)
        let year = date.getFullYear()
        /* 在日期格式中，月份是从0开始的，因此要加0
         * 使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
         * */
        let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
        let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
        let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
        let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
        let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
        // 拼接
        return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
      },
      // 详细信息
//     detail: function(id, width, height) {
//       // table.set();
//       var _url = $.operate.detailUrl(id);
//       var _width = $.common.isEmpty(width) ? "800" : width;
//       var _height = $.common.isEmpty(height) ? ($(window).height() - 50) : height;
//       //如果是移动端，就使用自适应大小弹窗
//       if ($.common.isMobile()) {
//         _width = 'auto';
//         _height = 'auto';
//       }
//       var options = {
//         title: "详细",
//         width: _width,
//         height: _height,
//         url: process.env.VUE_APP_BASE_API + "/basicInformation/ship/importData",
//         skin: 'layui-layer-gray',
//         btn: ['关闭'],
//         yes: function (index, layero) {
//           layer.close(index);
//         }
//       };
//       $.modal.openOptions(options);
//     },
//
// // 详细访问地址
//     detailUrl: function(id) {
//       var url = "/404.html";
//       if ($.common.isNotEmpty(id)) {
//         url = table.options.detailUrl.replace("{id}", id);
//       } else {
//         var id = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
//         if (id.length == 0) {
//           $.modal.alertWarning("请至少选择一条记录");
//           return;
//         }
//         url = table.options.detailUrl.replace("{id}", id);
//       }
//       return url;
//     },
      // 案件类型字典翻译
      evtTypeFormat(row, column) {
        return this.selectDictLabel(this.evtTypeOptions, row.evtType)
      },
      // 案件来源字典翻译
      evtSourceFormat(row, column) {
        return this.selectDictLabel(this.evtSourceOptions, row.evtSource)
      },
      // 案件状态字典翻译
      evtStateFormat(row, column) {
        return this.selectDictLabel(this.evtStateOptions, row.evtState)
      },
      // 违法、违规信息字典翻译
      violationFormat(row, column) {
        return this.selectDictLabel(this.violationOptions, row.violation)
      },
      // 管辖区字典翻译
      areaNameFormat(row, column) {
        return this.selectDictLabel(this.areaNameOptions, row.areaName)
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          evtLevel: undefined,
          evtType: undefined,
          evtSource: undefined,
          evtDate: undefined,
          evtAddress: undefined,
          evtContent: undefined,
          evtState: undefined,
          violation: undefined,
          areaName: undefined,
          x: undefined,
          y: undefined,
          reporterObjectId: undefined,
          reporterObjectName: undefined,
          reporterTime: undefined,
          delFlag: undefined,
          createById: undefined,
          createBy: undefined,
          createTime: undefined,
          updateById: undefined,
          updateBy: undefined,
          updateTime: undefined
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加案件信息'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const id = row.id || this.ids
        getEvent(id).then(response => {
          this.form = response.data
          this.open = true
          this.title = '修改案件信息'
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateEvent(this.form).then(response => {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addEvent(this.form).then(response => {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        this.$confirm('是否确认删除案件信息编号为"' + ids + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delEvent(ids)
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams1 = {...this.queryParams}
        queryParams1.pageSize = this.total
        queryParams1.pageNum = 1
        // queryParams.isAsc = 'asc'
        this.$confirm('是否确认导出所有案件信息数据项?', "警告", {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return exportEvent(queryParams1)
        }).then(response => {
          this.download(response.msg)
        })
      }
    }
  }
</script>
